package com.jingli.pile.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingli.pile.entity.SupConnectorInfo;
import com.jingli.pile.entity.SupEquipmentInfo;
import com.jingli.pile.entity.SupStationInfo;
import com.jingli.pile.request.StationQueryRequest;
import com.jingli.pile.service.SupConnectorInfoService;
import com.jingli.pile.service.SupEquipmentInfoService;
import com.jingli.pile.service.SupStationInfoService;
import com.jingli.pile.mapper.SupStationInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author abdul
* @description 针对表【sup_station_info】的数据库操作Service实现
* @createDate 2023-11-16 13:40:13
*/
@Service
public class SupStationInfoServiceImpl extends ServiceImpl<SupStationInfoMapper, SupStationInfo>
    implements SupStationInfoService{
    @Autowired
    private SupEquipmentInfoService equipmentInfoService;
    @Autowired
    private SupConnectorInfoService connectorInfoService;
    @Override
    public Page<SupStationInfo> queryStationInfo(StationQueryRequest stationQueryRequest) {
        Page<SupStationInfo> objectPage = Page.of(1, 10);
        if (null!=stationQueryRequest.getPageNo()&&null!=stationQueryRequest.getPageSize()){
            objectPage = Page.of(stationQueryRequest.getPageNo(),stationQueryRequest.getPageSize());
        }
        QueryWrapper<SupStationInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneEmpty(stationQueryRequest.getOperatorId())){
            queryWrapper.eq("operator_id",stationQueryRequest.getOperatorId());
        }
        if (StringUtils.isNoneEmpty(stationQueryRequest.getLastQueryTime())){
            queryWrapper.ge("last_update_time",stationQueryRequest.getLastQueryTime());
        }
        if (StringUtils.isNoneEmpty(stationQueryRequest.getStationIDs())){
            queryWrapper.in("station_id",stationQueryRequest.getStationIDs());
        }
        Page<SupStationInfo> page = page(objectPage, queryWrapper);
        List<SupStationInfo> records = page.getRecords();
        if (!records.isEmpty()){
            for (SupStationInfo stationInfo:records) {
                QueryWrapper<SupEquipmentInfo> equipmentInfoQueryWrapper = new QueryWrapper<>();
                equipmentInfoQueryWrapper.eq("station_id",stationInfo.getStationId());
                List<SupEquipmentInfo> supEquipmentInfos = equipmentInfoService.list(equipmentInfoQueryWrapper);
                for (SupEquipmentInfo equipment : supEquipmentInfos) {
                    QueryWrapper<SupConnectorInfo> connectorInfoQueryWrapper = new QueryWrapper<>();
                    connectorInfoQueryWrapper.eq("equipment_id",equipment.getEquipmentid());
                    List<SupConnectorInfo> list = connectorInfoService.list(connectorInfoQueryWrapper);
                    equipment.setConnectorInfos(list);
                }
                stationInfo.setEquipmentInfos(supEquipmentInfos);
                stationInfo.setMatchCars("出租车,物流车");
                stationInfo.setPictures("");
            }
        }
        page.setRecords(records);
        return page;
    }
}




